// 蛋糕
ajax({
    url:"/sa/cake-list.json",
    success:function(res){
        res=JSON.parse(res);
        console.log(res);
        dangao.innerHTML=render(res.data.cake);
    }
})
ajax({
    url:"/sa/details.json",
    success:function(res){
        res=JSON.parse(res);
        console.log(res);
        danjuan.innerHTML=render(res.data.hai)
    }
})

//全国
ajax({
    url:"/sa/local.json",
    success:function(res){
        res=JSON.parse(res);
        console.log(res);
        loc1.innerHTML=locat(res.data.cake);
    }
})
var dangao=document.querySelector(".dangao");
var danjuan=document.querySelector(".danjuan");
function render(data) {
    return data
      .map(function (item) {
        return `<div class="dang">
          <div class="sheng">
              <img class="img" index="${item.id}"  src="${item.ims}" alt="">
          </div>
          <div class="xia">
            <img src="${item.ima}" alt="" class="img1"  index="${item.id}">
        </div>
          <div class="kuan">
              <h4 class="guo">${item.name}</h4>
              <span class="jia">￥${
                item.ke ? item.prices + "/" + item.ke + "克" : item.prices
              }</span>
          </div>
      </div>`;
      })
      .join("");
  }

  var loc1=document.querySelector(".loc1");
  function locat(data){
    return data.map(function(item){
        return ` <div class="loc1">
        <div class="dang">
          <div class="sheng">
            <img class="img" index="${item.id}"
              src="${item.img}"
              alt=""
            />
          </div>
          <div class="xia">
          <img src="${item.ima}" alt="" class="img1"  index="${item.id}">
      </div>
          <div class="kuan">
            <h4 class="guo">${item.name}</h4>
            <span class="jia">特惠价￥${item.prices}起</span>
          </div>
        </div>
      </div>`
    }).join('');
  }


var center=document.querySelector(".center");
center.onclick=function(e){
  var e=e||window.event;
  if(e.target.classList.contains("img1")){
    var id=e.target.getAttribute("index");
    console.log(id);
    location.href="../html/detailes.html?id="+id;
  }
  // console.log(1);
}
// 回到顶部
var dingbu = document.querySelector(".dingbu");
window.onscroll = function () {
  var height=window.innerHeight+scrollY;
  if(height>window.innerHeight){
    dingbu.style.display = "block";
    dingbu.onclick = function () {
      scrollTo({ top: 0, behavior: "smooth" });
    };
  }
    else{
      dingbu.style.display = "none";
    }
  };